﻿using System;
using System.ComponentModel;
using System.Collections.ObjectModel;
using System.Windows;
using Microsoft.Practices.Prism.Commands;
using WpfPostgresql.Model;

namespace WpfPostgresql.ViewModel
{
    class SkladViewModel : INotifyPropertyChanged
    {


        private ObservableCollection<Databases.Tovar> _tovars;
        public ObservableCollection<Databases.Tovar> Tovars
        {
            get { return _tovars; }
            set { _tovars = value; RaisePropertyChanged("Tovars"); }
        }

        private Databases.Tovar _newTovar;
        public Databases.Tovar NewTovar
        {
            get { return _newTovar; }
            set { _newTovar = value; RaisePropertyChanged("NewTovar"); }
        }

        public SkladViewModel()
        {
            _addCommand = new DelegateCommand(AddNewTovar);
            _delCommand = new DelegateCommand<Databases.Tovar>(DelTovar);
            _addCol = new DelegateCommand<Databases.Tovar>(AddCol);
            NewTovar = new Databases.Tovar{N=0, SkladID = 1};
            GetTovars();
        }

        public void GetTovars()
        {
            Tovars = new ObservableCollection<Databases.Tovar>(Databases.GetTovarList(this));
        }
        private readonly DelegateCommand _addCommand;
        public DelegateCommand AddCommand
        {
            get
            {
                return _addCommand;
            }
        }
        private void AddNewTovar()
        {
            Databases.AddTovar(NewTovar);
            NewTovar = new Databases.Tovar { N = 0, SkladID = 1 };
            GetTovars();
        }

        private readonly DelegateCommand<Databases.Tovar> _delCommand;
        public DelegateCommand<Databases.Tovar> DelCommand
        {
            get
            {
                return _delCommand;
            }
        }
        private void DelTovar(Databases.Tovar tovar)
        {
            try
            {
                Databases.DeleteTovarById(tovar.ID);
                tovar.ParentVm.GetTovars();
            }
            catch (Exception)
            {
                MessageBox.Show("Удаляемый товар находиться в магазине! (удалите его из магазина) ");
            }

        }

        private readonly DelegateCommand<Databases.Tovar> _addCol;
        public DelegateCommand<Databases.Tovar> AddColCommand
        {
            get
            {
                return _addCol;
            }
        }
        private void AddCol(Databases.Tovar tovar)
        {
            Databases.AddColTovarById(tovar.ID, tovar.AddedN);
            tovar.ParentVm.GetTovars();
        }

        

        public event PropertyChangedEventHandler PropertyChanged;
        private void RaisePropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}
